mui.ready(function() {
	$.extend({
		getParameter: function(key) {
			var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");
			var params = window.location.search.substr(1).match(reg);
			if(params != null) {
				return unescape(params[2]);
			} else {
				return null;
			}
		},
		getCurrDate:function(dateTemp){
			var rDate = dateTemp || new Date();
			// var year = rDate.getFullYear();  
		 //   var month = rDate.getMonth() + 1;  
		 //   if (month < 10) month = "0" + month;  
		 //   var date = rDate.getDate();  
		 //   if (date < 10) date = "0" + date;  
			// var hour = rDate.getHours();
			// if (hour < 10) hour = "0"+hour;
			// var minute = rDate.getMinutes();
			// if (minute < 10) minute = "0"+minute;
			// var second = rDate.getSeconds();
			// if (second < 10) second="0"+second;

			// return (year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second);
			return rDate;
		},
		getDate:function(dateTemp, days) {
		   // var dateTemp = dateTemp.split("-");  
		   var nDate = dateTemp || new Date();//new Date(dateTemp[1] + '-' + dateTemp[2] + '-' + dateTemp[0]); //转换为MM-DD-YYYY格式
		   if(typeof nDate=='string') nDate = new Date(nDate);
		   var millSeconds = Math.abs(nDate) + (days * 24 * 60 * 60 * 1000);  
		   var rDate = new Date(millSeconds);  
		 //   var year = rDate.getFullYear();  
		 //   var month = rDate.getMonth() + 1;  
		 //   if (month < 10) month = "0" + month;  
		 //   var date = rDate.getDate();  
		 //   if (date < 10) date = "0" + date;  
			// var hour = rDate.getHours();
			// if (hour < 10) hour = "0"+hour;
			// var minute = rDate.getMinutes();
			// if (minute < 10) minute = "0"+minute;
			// var second = rDate.getSeconds();
			// if (second < 10) second="0"+second;

		   // return (year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second);  
		   return rDate;
		},
		getFormData:function(id){
			var formDataArr = $('form[id="'+id+'"]').serializeArray();
			var _formDataObj = {};
			formDataArr.forEach(function(item) {
				_formDataObj[item.name] = item.value;
			});
			return _formDataObj;
		}
	});

	mui('.mui-scroll-wrapper').scroll();
	mui('.footer').on('tap', 'a', function(e) {
		var id = this.getAttribute("data-id");
		if(id == 1){
			window.localStorage.setItem('dahua_facets', "");
			window.localStorage.setItem('dahua_sid', "");
		}
		var url = this.getAttribute('href');
		mui.openWindow({
			url: url
		});
	});
	
	//设置用户信息
	var userId = window.localStorage.getItem("dahua_userId");
	//	alert(userId);
	if(null == userId || "" == userId) {
		$("#uid").val("");
	} else {
		$("#uid").val(userId);
		// var isPartner=window.localStorage.getItem("dahua_isPartner");
		// if(null==isPartner){
			// mui.ajax('/user/isPartner.do', {
			// 	data: {
			// 		uid: userId
			// 	},
			// 	dataType: 'json',
			// 	success: function(flag) {
					// window.localStorage.setItem('dahua_isPartner', flag);
					// if(flag){
					// 	$("#partner").removeClass("mui-hidden")
					// }
			// 	}
			// });
		// }else{
		// 	if("true"==isPartner){
				$("#partner").removeClass("mui-hidden")
		// 	}
		// }
	}
	
	var _openWindow=mui.openWindow,_permissions=[];
	// Ryan
	if(window.location.hostname=='localhost') {
		mui.ajax('/permissions.json', {
			data: {
				uid: userId||''
			},
			dataType: 'json',
			success: function(response) {
				// console.log(response.object);
				if(1==response.status) {
					_permissions=response.object;
				}
			}
		});
	}
	mui.extend({
		openWindow:function(url, id, options){
			// Ryan
			if(window.location.hostname=='localhost'){
				if(_permissions.indexOf(url.url)==-1){
					_openWindow(url, id, options);
				}else {
					mui.alert($.i18n.prop('permissions_msg'));
				}
			}else {
				_openWindow(url, id, options);
			}
		},
		hasPermissions:function(url) {
			// Ryan
			if(window.location.hostname=='localhost'){
				if(_permissions.indexOf(url.url)==-1) {
					return true;
				}else {
					mui.alert($.i18n.prop('permissions_msg'));
					return false;
				}
			}else {
				return true;
			}
		},
		nativeback:function(){
			var _a = $.getUrlParam("a");
			if(!_a) {
				$('.header .login').removeClass('mui-action-back');
				$('.header .home').hide();
				mui(".header").on('tap', '.login', function() {
					var url = "dahua://nativeback";
					// var url = "dahua://login";
					mui.openWindow({
						url: url
					});
				});
			}
		}
	});
});

//图片错误
var imgError= function(obj){
	$(obj).attr("src","../../resources/images/errorImg.png");
}


function logon(userId, parentUserId) {
	if('null'!=userId){
		window.localStorage.setItem('dahua_userId', userId);
		window.localStorage.setItem('dahua_userId1', parentUserId);
	}else{
		window.localStorage.setItem('dahua_userId', "");
		window.localStorage.setItem('dahua_userId1', "");
	}
}
// 
// function setLanguage(language) {
// 	if('null'!=language){
// 		window.localStorage.setItem('lang', language);
// 	}else{
// 		window.localStorage.setItem('lang', "en");
// 	}
// }
// 
function logout() {
	window.localStorage.removeItem("dahua_userId");
	window.localStorage.removeItem("dahua_userId1");
	window.localStorage.removeItem("dahua_isPartner");
	var url = "../product/category.html";
	mui.openWindow({
		url: url
	});
}

function reload() {
	window.location.reload();
}
//打开广播列表
function openMessage(){
	var url = "../message/list.html?channel=1";
	mui.openWindow({
		url: url
	});
}
//打开区域政策列表
function openPolicy(){
	var url = "../channelpolicy/list.html";
	mui.openWindow({
		url: url
	});
}



//filters
//转义
avalon.filters.replace = function (value) {
	return String(value).replace(/&quot;/g, '"').replace(/&#39;/g, '\'').replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&amp;/g, '&');
};
//换行
avalon.filters.wrap = function (value) {
	return value;
};

//状态转义
avalon.filters.getStatus = function (value) {
	if(value==0){
		return $.i18n.fail;
	}else{
		return $.i18n.success;
	}
};
(function($){
	$.fn.extend({
		openLoadForm:function(msg,callback){
			if(typeof msg=="function") {
				callback=msg;
				msg={};
			}
			$(this).on('click', function(){
				var index=$.openLoadForm(msg);
				if(typeof callback=="function") {
					callback(index);
				}
			});
		}
	});
	$.extend({
		openLoadForm:function(msg){
			if(typeof msg=="string") {
				msg={"msg":msg};
			}
			var option=$.extend({},defoption,msg);
			var uuid="xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c){
				var r=Math.random()*16|0, v=c=='x'?r:(r&0x3|0x8);
				return v.toString(16);
			});
			//display: flex;justify-content: center;
			// var str='<div class="loadForms" id="'+uuid+'" style="display: flex;justify-content: space-evenly;cursor:not-allowed;width:100%;height:100%;position:fixed;z-index:9999;background-color:rgba(235,235,235,0.55);top:0px;">'+
			// 	'	<div id="box_'+uuid+'" style="user-select:none;opacity:1;width:auto;height:auto;display:block;padding:0px;border:1px solid #ccc;font-size:18px;position:fixed;background-color:white;margin-top:30%; box-shadow:1px 1px #cecece">'+
			// 	'		<div style="float:left;">'+
			// 	'			<img style="margin: 12px 10px;width:25px;" src="'+option["gif"]+'" _height="64" _width="64">'+
			// 	'		</div>'+
			// 	'		<div id="content_'+uuid+'" style="display:block;float:left;padding-top:15px;padding-right:37px;">'+option['msg']+'</div>'+
			// 	'	</div>'+
			// 	'</div>';			
			var str='<div class="loadForms" id="'+uuid+'" style="space-evenly;cursor:not-allowed;width:100%;height:100%;position:fixed;z-index:9999;background-color:rgba(235,235,235,0.55);top:0px;">'+
				'	<div id="box_'+uuid+'" style="user-select:none;opacity:1;width:150px;height:150px;border-radius: 10px;display: flex;flex-direction: column;align-items: center;margin-right: auto;margin-left: auto;text-align: center;padding:0px;border:1px solid #ccc;font-size:18px;background-color:#000;opacity: 0.6;color:white;margin-top:30%; box-shadow:1px 1px #cecece">'+
				'		<div style="">'+
				'			<img style="margin: 30px 10px 10px 10px;width:35px;" src="'+option["gif"]+'" _height="64" _width="64">'+
				'		</div>'+
				'		<div id="content_'+uuid+'" style="display:block;float:left;padding-top:15px;">'+option['msg']+'</div>'+
				'	</div>'+
				'</div>';
			$("body").append(str);
			// $('#content_'+uuid).on('resize', function(){
				// var width=$('body').width();
				// var tinbW=$('#content_'+uuid).width();
				// console.log(width, tinbW);
				// $("#box_"+uuid).css("left", width/2-tinbW/2);
				// alert(tinbW);
			// });
			return uuid;
		},
		closeLoadForm:function(index){
			if(!index) {
				$('.loadForms').remove();
				return ;
			}
			$('#'+index).remove();
		},
		getUrlParam:function(name){
			var reg=new RegExp("(^|&)"+name+"=([^&]*)(&|$)");
			var r=window.location.search.substr(1).match(reg);
			// console.log(r, window.location.search.substr(1), window.location.search);
			if(r!=null)return unescape(r[2]);
			return null;
		}
	});
	var defoption={
		gif:'../../resources/images/wait.gif',
		msg:'加载中，请稍后'
	}
})(window.jQuery);